package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.resource.FlavorService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;

@RestController
@RequestMapping("/flavor")
public class FlavorController {

    @Autowired
    private FlavorService flavorService;

    /**
     * 列表
     * 
     * @param page
     * @param rows
     * @param params
     * @param sorter
     * @param simple
     * @param user
     * @return
     */
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
            @RequestParam(value = Common.ROWS, required = false) Integer rows,
            @RequestParam(value = Common.PARAMS, required = false) String params,
            @RequestParam(value = Common.SORTER, required = false) String sorter,
            @RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
            @Value(Common.REQ_USER) RequestUser user) {
        @SuppressWarnings("unchecked")
        Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
        return flavorService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);
    }

    /**
     * 查询配置
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/listFlavor", method = { RequestMethod.POST })
    public BsmResult listFlavor(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        Long id = object.getLong(Common.ID);
        String name = object.getString(Common.NAME);
        return flavorService.listFlavor(id, name);
    }

    /**
     * 详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
    public BsmResult detail(@PathVariable(value = Common.ID) Long id) {
        return flavorService.detail(id);
    }

    /**
     * 创建
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/create", method = { RequestMethod.POST })
    public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return flavorService.create(params, user.getId());
    }

    /**
     * 删除
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
    public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return flavorService.remove(id, user.getId());
    }

}
